﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Game.Terrain
{
    public class TerrainBits 
    {
        private readonly Collection<TerrainBit> _Bits = new Collection<TerrainBit>();
        
        public void Add(TerrainBit bit)
        {
            _Bits.Add(bit);
        }
        
        public bool Contains(TerrainBit bit)
        {
            return _Bits.Contains(bit);
        }
        
        public IEnumerable<TerrainBit> GetEnumerator()
        {
            return _Bits;
        }
        
        public ReadOnlyTerrainBits ToReadOnly()
        {
            return new ReadOnlyTerrainBits(this);
        }
    }
    
    public class ReadOnlyTerrainBits
    {
        private readonly Collection<TerrainBit> _Bits;
        
        public ReadOnlyTerrainBits(TerrainBits bits)
        {
            List<TerrainBit> list = new List<TerrainBit>(bits.GetEnumerator());
            _Bits = new Collection<TerrainBit>(list);
        }
        
        public bool Contains(TerrainBit bit)
        {
            return _Bits.Contains(bit);
        }
        
        public IEnumerable<TerrainBit> GetEnumerator()
        {
            return _Bits;
        }

        public double Cost
        {
            get
            {
                double c = 0;
                foreach (TerrainBit bit in _Bits)
                {
                    c += bit.Cost;
                }
                return c;
            }
        }
        
        public override string ToString()
        {
            string s = "[";
            foreach (TerrainBit bit in _Bits) 
            {
                s += "{" + bit.ToString() + "}";
            }
            s += "]";
            return s;
        }

    }
    
}
